package org.boge.sharpdoc.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.boge.sharpdoc.dao.CustomerDao;
import org.boge.sharpdoc.bean.Customer;
import org.boge.sharpdoc.bean.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CustomerService {
    @Autowired
    private CustomerDao customerDao;

    public Boolean judgeExist(Customer customer) {
        return customerDao.findWhetherExist(customer.getPhone());
    }

    public void register(Customer customer) {
        customerDao.addCustomer(customer);
    }

    public Customer login(Customer customer) {
        Long tmp = customerDao.findOfLogin(customer);
        if (tmp != 0)
            return customerDao.findByPhone(tmp);
        else
            return null;
    }

    public Integer changeAvatar(Customer customer) {
        return customerDao.updateAvatar(customer);
    }

    public Integer changePassword(Customer customer) {
        return customerDao.updatePassword(customer);
    }

    public Integer changeInfo(Customer customer) {
        return customerDao.updateInfo(customer);
    }

    public Integer bindEmail(Customer customer) {
        return customerDao.updateEmail(customer);
    }

    public Integer getCustomerCount() {
        return customerDao.findCount();
    }

    public Customer getCustomerByPhone(Long phone) {
        return customerDao.findByPhone(phone);
    }

    public Customer decIntegral(Customer customer, Integer price) {
        customerDao.updateIntegral(customer.getPhone(), customer.getIntegral() - price);
        return customerDao.findByPhone(customer.getPhone());
    }

    public Integer addIntegral(Document docid) {
        return customerDao.updateIntegral(docid.getUploaduser().getPhone(), docid.getUploaduser().getIntegral() + 2);
    }

    public PageInfo<Customer> getAllCustomersByPage(Integer index, Integer pageSize) {
        return PageHelper.startPage(index, pageSize).doSelectPageInfo(() -> customerDao.findAll());
    }

    public Integer changeStatus(Long phone, String status) {
        return customerDao.updateStatus(phone, status);
    }
}